<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .box {
      width: 100px;
      height: 100px;
      background: palegreen;
      position: fixed;
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
</html>
<script>
// 英文：
  // distance  距离
  // step 步幅

const box = document.querySelector('.box');

// 案例：从上向下，移动 400 像素   +  从左往右 移动600像素

function move(box, target) {


  box.addEventListener('click', e => {

    Object.entries(target) // 返回值：[['top', 400], ['left', 600]]
      .forEach(([type, targetDistance]) => { // 循环第一次: ['top', 400]
        let distance = 0;                    // 循环第二次: ['left', 600]
        const step = 5;
        const id = setInterval(() => {
          distance += step;

          if (distance > targetDistance) {
            clearInterval(id)
          }
          box.style[type] = distance + 'px';
        }, 1000 / 60);
      });

  });
}

move(box, {
  top: 400,
  left: 600
})




</script>